package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.dubbo.mappers.UmsAdminLoginLogMapper;
import com.mall.dubbo.mappers.UmsAdminRolePermissionRelationMapper;
import com.mall.dubbo.mappers.UmsAdminRoleRelationMapper;
import com.tanhua.model.domain.UmsAdminLoginLog;
import com.tanhua.model.domain.UmsAdminPermissionRelation;
import com.tanhua.model.domain.UmsAdminRoleRelation;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.dubbo.mappers.UmsAdminMapper;
import com.tanhua.model.domain.UmsAdmin;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.regex.Pattern;

@DubboService
public class UmsAdminApiImpl implements UmsAdminApi {

    @Autowired
    private UmsAdminMapper umsAdminMapper; //用户表

    @Autowired
    private UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;  //角色和用户表

    @Autowired
    private UmsAdminLoginLogMapper umsAdminLoginLogMapper;  //角色日志表

    @Autowired
    private UmsAdminRolePermissionRelationMapper umsAdminRolePermissionRelationMapper;


    /**
     * 根据 账号 密码查找
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public UmsAdmin findOne(String username, String password) {
        LambdaQueryWrapper<UmsAdmin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UmsAdmin::getUsername, username).eq(UmsAdmin::getPassword, DigestUtils.md5DigestAsHex(password.getBytes()));
        UmsAdmin ums_admin = umsAdminMapper.selectOne(lambdaQueryWrapper);
        return ums_admin;
    }

    /**
     * 根据 id查找账号
     *
     * @param id
     * @return
     */
    @Override
    public UmsAdmin findById(Long id) {
        return umsAdminMapper.selectById(id);
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page list(String keyword, Integer pageNum, Integer pageSize) {
        Page<UmsAdmin> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsAdmin> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(keyword), UmsAdmin::getUsername, keyword)
                .or().like(StringUtils.isNotEmpty(keyword), UmsAdmin::getNickName, keyword);
        return umsAdminMapper.selectPage(page, lqw);
    }

    /**
     * 用户注册
     *
     * @param umsAdmin
     * @return
     */
    @Override
    public boolean register(UmsAdmin umsAdmin) {
        //查询名字是否重复
        LambdaQueryWrapper<UmsAdmin> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(umsAdmin.getUsername()), UmsAdmin::getUsername, umsAdmin.getUsername());
        Integer integer = umsAdminMapper.selectCount(lqw);
        //名字重复
        if (integer > 0) {
            throw new BusinessException(ErrorResult.resourceError());
        }
        //验证邮箱是否则正确
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        if (!regex.matcher(umsAdmin.getEmail()).matches()) {
            throw new BusinessException(ErrorResult.EmaError());
        }
        Date date = new Date(System.currentTimeMillis());
        umsAdmin.setCreateTime(date);
        umsAdmin.setPassword(DigestUtils.md5DigestAsHex(umsAdmin.getPassword().getBytes()));
        if (umsAdminMapper.insert(umsAdmin) > 0) {
            return true;
        }
        return false;

    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        if (id == null) {
            return false;
        }
        //只有禁用账号才可以删除
        if (umsAdminMapper.selectById(id).getStatus() == 1) {
            throw new BusinessException("启用状态不给删除!");
        }
        //删除角色表
        umsAdminMapper.deleteById(id);
        //删除角色和用户表
        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UmsAdminRoleRelation::getAdminId, id);
        umsAdminRoleRelationMapper.delete(queryWrapper);
        //删除角色日志表
        LambdaQueryWrapper<UmsAdminLoginLog> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(UmsAdminLoginLog::getAdminId, id);
        umsAdminLoginLogMapper.delete(queryWrapper1);
        //删除表
        LambdaQueryWrapper<UmsAdminPermissionRelation> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.in(UmsAdminPermissionRelation::getAdminId, id);
        umsAdminRolePermissionRelationMapper.delete(queryWrapper2);
        return true;
    }

    //修改用户状态
    @Override
    public boolean updateStatus(Integer id, Integer status) {
        LambdaQueryWrapper<UmsAdmin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(null != status, UmsAdmin::getId, id);
        UmsAdmin umsAdmin = umsAdminMapper.selectOne(lambdaQueryWrapper);
        if (umsAdmin != null) {
            umsAdmin.setStatus(status);
            umsAdminMapper.updateById(umsAdmin);
            return true;
        }
        return false;
    }

    /**
     * 修改用户信息
     */
    @Override
    public boolean update(UmsAdmin umsAdmin) {
        if (umsAdmin == null) {
            return false;
        }
        if (umsAdmin.getPassword().length() < 30) {
            umsAdmin.setPassword(DigestUtils.md5DigestAsHex(umsAdmin.getPassword().getBytes()));
        }
        umsAdminMapper.updateById(umsAdmin);
        return true;
    }

    public static void main(String[] args) {
        System.out.println(DigestUtils.md5DigestAsHex("123456".getBytes()));
    }
}
